﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Project_V1;

namespace Project_V1
{
    public partial class FrmParaModify : Form
    {
        public static string input = string.Empty;
        public static bool confirm = false;
        public FrmParaModify()
        {
            InitializeComponent();
        }

        string VariableName = string.Empty;

        //在参数设置界面FrmParamSet,使用有参数构造方法传值
        public FrmParaModify(string InitialValue, string Variable)
        {
            InitializeComponent();
            if (InitialValue == "") InitialValue = 0.ToString();
            this.lbl_Initial.Text = InitialValue;//读取现在的值显示在Label
            VariableName = Variable;
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //点击确定按钮后,将TextBox内的值写入PLC,(需要修改实时读取和停止线程)
        private void btn_Sure_Click(object sender, EventArgs e)
        {
            input = txt_Modify.Text;
            int dotCount = input.Count(c => c == '.');
            if (dotCount >= 2 || input.IndexOf(".") == 0 || input.IndexOf(".") == input.Length - 1)
            {
                MessageBox.Show("数字输入错误");
                return;
            }

            Beckhoff_PLC.IsWriting = true;
            Thread.Sleep(500);
            string temptype = "";

            temptype = CommonMethods.CurrentType[VariableName];
            string str = CommonMethods.CurrentType[VariableName].ToString();
            if (CommonMethods.CurrentType[VariableName] == "float")
            {
                if (Beckhoff_PLC.WriteVariable(VariableName, float.Parse(this.txt_Modify.Text.Trim())))//将每个变量的变量名传来,在Tag中
                {
                   // MessageBox.Show("修改成功！", "修改提示");
                }
                else
                {
                    MessageBox.Show("修改失败！", "修改提示");
                }
            }

            if (CommonMethods.CurrentType[VariableName] == "ushort")
            {
                if (Beckhoff_PLC.WriteVariable(VariableName, ushort.Parse(this.txt_Modify.Text.Trim())))//将每个变量的变量名传来,在Tag中
                {
                    //MessageBox.Show("修改成功！", "修改提示");
                }
                else
                {
                    MessageBox.Show("修改失败！", "修改提示");
                }
            }

            if (CommonMethods.CurrentType[VariableName] == "int")
            {
                if (Beckhoff_PLC.WriteVariable(VariableName, Int16.Parse(this.txt_Modify.Text.Trim())))//将每个变量的变量名传来,在Tag中
                {
                    //MessageBox.Show("修改成功！", "修改提示");
                }
                else
                {
                    MessageBox.Show("修改失败！", "修改提示");
                }
            }

            if (CommonMethods.CurrentType[VariableName] == "Float")
            { 
                if (Beckhoff_PLC.WriteVariable(VariableName, float.Parse(this.txt_Modify.Text.Trim())))//将每个变量的变量名传来,在Tag中
                {
                    //MessageBox.Show("修改成功！", "修改提示");
                }
                else
                {
                    MessageBox.Show("修改失败！", "修改提示");
                }
            }

            if (CommonMethods.CurrentType[VariableName] == "UInt32")
            {
                if (Beckhoff_PLC.WriteVariable(VariableName, UInt32.Parse(this.txt_Modify.Text.Trim())))//将每个变量的变量名传来,在Tag中
                {
                    //MessageBox.Show("修改成功！", "修改提示");
                }
                else
                {
                    MessageBox.Show("修改失败！", "修改提示");
                }

            }
             
            Beckhoff_PLC.IsWriting = false;
            Thread.Sleep(1000);

            DataSync.SavePowerOffValueToCSV(System.Environment.CurrentDirectory + "\\ConfigFile\\Cache.csv");

          
            this.Close();

        }

        //Enter
        private void txt_Modify_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btn_Sure_Click(null, null);
            }
        }

        private void FrmParaModify_Load(object sender, EventArgs e)
        {

        }


        void btn_Click(object sender, EventArgs e)
        {
            Button B = (Button)sender;
            string buttonText = B.Text;
            //int selectionStart = txt_Modify.SelectionStart; // 保存当前焦点位置
            int selectionStart = 0;// txt_Modify.SelectionStart; // 保存当前焦点位置
            txt_Modify.SelectionStart = txt_Modify.Text.Length;
            selectionStart = txt_Modify.SelectionStart; // 保存当前焦点位置


            txt_Modify.Text = txt_Modify.Text.Insert(selectionStart, buttonText); // 在焦点位置插入按钮文字
            txt_Modify.SelectionStart = selectionStart + buttonText.Length; // 设置焦点位置
            txt_Modify.Focus(); // 重新设置焦点到文本框
        }
        private void button8_Click(object sender, EventArgs e)
        {
            txt_Modify.Text = string.Empty;
            txt_Modify.Focus();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (txt_Modify.Text.Length == 0)
            {
                return;
            }
            txt_Modify.SelectionStart = txt_Modify.Text.Length;
            int selectionStart = txt_Modify.SelectionStart;
            txt_Modify.Text = txt_Modify.Text.Remove(selectionStart - 1, 1);
            txt_Modify.SelectionStart = selectionStart - 1;
            txt_Modify.Focus();
        }

        private void txt_Modify_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != '.' && !char.IsControl(e.KeyChar))
            {
                e.Handled = true; // 取消输入
            }

            // 确保只能输入一个小数点
            if (e.KeyChar == '.' && ((TextBox)sender).Text.Contains('.'))
            {
                e.Handled = true; // 取消输入
            }
        }

        private void txt_Modify_TextChanged(object sender, EventArgs e)
        {
            var minv = 0;
            var maxv = 0;

            if(VariableName == "_03_HMI.FeedMachRivetOutOverTime") { minv = 0;maxv = 40000; }
            //if (VariableName == "_03_HMI.MaxFeedingTime") { minv = 0; maxv = 10000; }
            if (VariableName == "_03_HMI.EnableVacuoValveLater_SampleTime1") { minv = 0; maxv = 4000; }
            if (VariableName == "_03_HMI.EnableVacuoValveLater_SampleTime2") { minv = 0; maxv = 4000; }
            if (VariableName == "_03_HMI.VacuoRchLater_SampleTime") { minv = 0; maxv = 4000; }
            if (VariableName == "_03_HMI.CachePlaceRivetInOverTime") { minv = 0; maxv = 4000; }
            if (VariableName == "_03_HMI.CachePlaceRivetAtOverTime") { minv = 0; maxv = 20000; }
            if (VariableName == "_03_HMI.CachePlaceRivetAtHeartbeatTime") { minv = 0; maxv = 1000; }
            if (VariableName == "_03_HMI.CachePlaceRivetOutOverTime") { minv = 0; maxv = 40000; }
            if (VariableName == "_03_HMI.GunHeadRivetInOverTime") { minv = 0; maxv = 20000; }
            if (VariableName == "_03_HMI.GunHeadRivetIn_EnableVacuoValveDelayTime") { minv = 0; maxv = 2000; }
            if (VariableName == "_03_HMI.FeedTest_InhaleRivetMaxTime") { minv = 0; maxv = 4000; }
            if (VariableName == "_03_HMI.FeedTest_InhaleRivetIntervalTime") { minv = 0; maxv = 500; } 

            if (VariableName == "_03_HMI.FeedMachRivetOutOverTime" ||
                //VariableName == "_03_HMI.MaxFeedingTime" ||
                VariableName == "_03_HMI.EnableVacuoValveLater_SampleTime1" ||
                VariableName == "_03_HMI.EnableVacuoValveLater_SampleTime2" ||
                VariableName == "_03_HMI.VacuoRchLater_SampleTime" ||
                VariableName == "_03_HMI.CachePlaceRivetInOverTime" ||
                VariableName == "_03_HMI.CachePlaceRivetAtOverTime"  ||
                VariableName == "_03_HMI.CachePlaceRivetAtHeartbeatTime" ||
                VariableName == "_03_HMI.CachePlaceRivetOutOverTime" ||
                VariableName == "_03_HMI.GunHeadRivetInOverTime" ||
                VariableName == "_03_HMI.GunHeadRivetIn_EnableVacuoValveDelayTime" ||
                VariableName == "_03_HMI.FeedTest_InhaleRivetMaxTime" ||
                VariableName == "_03_HMI.FeedTest_InhaleRivetIntervalTime" 
                )
            {
                if (int.TryParse(txt_Modify.Text, out int value))
                {
                    if (value < minv)
                    {
                        txt_Modify.Text = minv.ToString();
                    }
                    else if (value > maxv)
                    {
                        txt_Modify.Text = maxv.ToString();
                    }
                }
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
